﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.Repository.Models;
using Zj.HttpCodeDemo.Domain.Entities;
using System.Transactions;
using Z.EntityFramework.Plus;
using System.Threading;

namespace Zj.HttpCodeDemo.ConsoleApp.Samples
{
    /// <summary>
    /// Concurrent sample.
    /// </summary>
    public class ConCurrentSample
    {
        public static void Sample()
        {
            using (var ctx = new TodoDbContext())
            {
                //var user = new User()
                //{
                //    UserName = "ZhangJian",
                //    Pwd = "p@ssw0rd"
                //};
                //ctx.User.Add(user);
                //ctx.SaveChanges();
                var opt = new TransactionOptions();
                opt.IsolationLevel = IsolationLevel.ReadCommitted;
                opt.Timeout = TimeSpan.FromSeconds(60);

                var opt2 = new TransactionOptions();
                opt.IsolationLevel = IsolationLevel.ReadUncommitted;
                opt.Timeout = TimeSpan.FromSeconds(60);

                AutoResetEvent reset = new AutoResetEvent(false);
                //using (var scope = new TransactionScope(TransactionScopeOption.Required, opt))
                //{
                //    ctx.User.Where(u => u.Pwd.Equals("p@ssw0rd")).Update(u => new User() { Pwd = "p@ssword" });
                //    Task.Factory.StartNew(() =>
                //    {
                //        try
                //        {
                //            var user = ctx.User.First();
                //            reset.Set();
                //        }
                //        catch (Exception ex)
                //        {
                //            Console.WriteLine(ex.Message);
                //        }
                //    });
                //    reset.WaitOne();
                //    reset.Dispose();
                //    //scope.Complete();
                //}
            }
        }
    }
}
